package com.hangzhou.xyj.ui.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.ActionModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.ui.activity.WebActivity;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.constants.DataKey;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 发现->活动 具体的条目
 * Created by Administrator on 2016/1/15.
 */
public class FindActionDetailAdapter extends BaseLoadingAdapter<ActionModel> {
    private static final int TYPE_DOUBLE_IMG = 2;//两张图片的条目
    private static final int TYPE_SINGLE_IMG = 1;//单张图片的条目
    private static final int TYPE_TRIPLE_IMG = 3;//三张图片的条目

    public FindActionDetailAdapter(RecyclerView recyclerView, List<ActionModel> dataList) {
        super(recyclerView, dataList);
    }

    @Override
    public void onBindNormalViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        switch (getItemViewType(position)) {
            case TYPE_SINGLE_IMG:
                MyViewHolderSingleImg holder = (MyViewHolderSingleImg) viewHolder;
                holder.setData(mDataList.get(position));
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, WebActivity.class);
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_FIND_ACTION.getValue());
                        intent.putExtra(DataKey.TITLE, "活动详情");
                        intent.putExtra(DataKey.OBJ_ID, mDataList.get(position).getId());
                        intent.putExtra(DataKey.INDEX, mDataList.get(position).getUrl());
                        intent.putExtra(DataKey.SHARE_CONTENT, mDataList.get(position).getIntroduce());
                        intent.putExtra(DataKey.SHARE_URL, mDataList.get(position).getShareUrl());
                        intent.putExtra(DataKey.SHARE_TITLE, mDataList.get(position).getName());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
            case TYPE_DOUBLE_IMG:
            case TYPE_TRIPLE_IMG:
            default:
                MyViewHolderTripleImg holderTripleImg = (MyViewHolderTripleImg) viewHolder;
                holderTripleImg.setData(mDataList.get(position));
                holderTripleImg.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, WebActivity.class);
                        intent.putExtra(DataKey.INTENT_TAG, EIntentTagType.TAG_FIND_ACTION.getValue());
                        intent.putExtra(DataKey.TITLE, "活动详情");
                        intent.putExtra(DataKey.OBJ_ID, mDataList.get(position).getId());
                        intent.putExtra(DataKey.INDEX, mDataList.get(position).getUrl());
                        intent.putExtra(DataKey.SHARE_TITLE, mDataList.get(position).getName());
                        intent.putExtra(DataKey.SHARE_CONTENT, mDataList.get(position).getIntroduce());
                        intent.putExtra(DataKey.SHARE_URL, mDataList.get(position).getShareUrl());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_SINGLE_IMG:
                return new MyViewHolderSingleImg(mLayoutInflater.inflate(R.layout.layout_find_action_item_single_img, parent, false));
            case TYPE_TRIPLE_IMG:
            case TYPE_DOUBLE_IMG:
            default:
                return new MyViewHolderTripleImg(mLayoutInflater.inflate(R.layout.layout_find_action_item_triple_img, parent, false));
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (mDataList.get(position) == null) {
            return TYPE_LOADING_ITEM;
        }
        switch (mDataList.get(position).getImgList().size()) {
            case TYPE_SINGLE_IMG:
                return TYPE_SINGLE_IMG;
            case TYPE_DOUBLE_IMG:
                return TYPE_DOUBLE_IMG;
            default:
                return TYPE_TRIPLE_IMG;
        }
    }


    /**
     * 第二种样式的View，多张图片
     */
    class MyViewHolderTripleImg extends RecyclerView.ViewHolder {
        MyTextView mTvTitle;//标题
        MyTextView mTvIntroduce;//介绍
        MyTextView mTvPartNum;//参与人数
        MyTextView mTvTime;//活动时间
        MyTextView mTvLocation;//地址
        MyImageView mIvImg1;//图片1
        MyImageView mIvImg2;//图片2
        MyImageView mIvImg3;//图片3

        public MyViewHolderTripleImg(View itemView) {
            super(itemView);
            mTvTitle = (MyTextView) itemView.findViewById(R.id.mtv_title);
            mTvIntroduce = (MyTextView) itemView.findViewById(R.id.mtv_introduce);
            mTvPartNum = (MyTextView) itemView.findViewById(R.id.mtv_part_num);
            mTvTime = (MyTextView) itemView.findViewById(R.id.mtv_time);
            mTvLocation = (MyTextView) itemView.findViewById(R.id.mtv_location);
            mIvImg1 = (MyImageView) itemView.findViewById(R.id.miv_image1);
            mIvImg2 = (MyImageView) itemView.findViewById(R.id.miv_image2);
            mIvImg3 = (MyImageView) itemView.findViewById(R.id.miv_image3);
        }


        public void setData(ActionModel data) {
            //设置标题
            mTvTitle.setText(data.getName());
            //设置介绍
            mTvIntroduce.setText(data.getIntroduce());
            //设置参与人数
            mTvPartNum.setText("" + data.getPartNum());
            //设置时间
            mTvTime.setText(data.getTime());
            //设置地点
            mTvLocation.setText(data.getLocation());
            //设置图片
            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.displayImage(data.getImgList().get(0), mIvImg1);
            imageLoader.displayImage(data.getImgList().get(1), mIvImg2);
            if (data.getImgList().size() == 3)
                imageLoader.displayImage(data.getImgList().get(2), mIvImg3);
        }
    }

    /**
     * 第一种样式的View，单张图片的
     */
    class MyViewHolderSingleImg extends RecyclerView.ViewHolder {
        MyImageView myImageView;//图片
        MyTextView mTvTime;//时间
        MyTextView mTvPartNum;//参与人数
        MyTextView mTvLocation;//地址
        MyTextView mTvTitle;//标题
        MyTextView mTvIntroduce;//介绍

        public MyViewHolderSingleImg(View itemView) {
            super(itemView);
            myImageView = (MyImageView) itemView.findViewById(R.id.miv_image);
            mTvTime = (MyTextView) itemView.findViewById(R.id.mtv_time);
            mTvPartNum = (MyTextView) itemView.findViewById(R.id.mtv_part_num);
            mTvLocation = (MyTextView) itemView.findViewById(R.id.mtv_location);
            mTvTitle = (MyTextView) itemView.findViewById(R.id.mtv_title);
            mTvIntroduce = (MyTextView) itemView.findViewById(R.id.mtv_content);
        }

        /**
         * 填充View需要展示的数据
         *
         * @param data
         */
        public void setData(ActionModel data) {
            //设置图片
            myImageView.displayImage(data.getImgList().get(0));
            //设置时间
            mTvTime.setText(data.getTime());
            //设置参与人数
            mTvPartNum.setText("" + data.getPartNum());
            //设置地址
            mTvLocation.setText(data.getLocation());
            //设置标题
            mTvTitle.setText(data.getName());
            //设置介绍
            mTvIntroduce.setText(data.getIntroduce());
        }
    }
}
